package com.knowledge.graph.job;

import com.knowledge.graph.esRepository.CourseESRepository;
import com.knowledge.graph.mapper.CourseMapper;
import com.knowledge.graph.model.entity.Course;
import com.knowledge.graph.model.entity.CourseES;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 开启定时任务，将数据库中课程表导入ES
 * @author jiwenlong
 * @date 2024/3/1 22:06
 * @version 1.0
 */
@Component
@Slf4j
public class CourseToES {

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private CourseESRepository courseESRepository;

    @Scheduled(fixedRate = 60 * 1000)
    public void run() {
        List<Course> courseList = courseMapper.listByUpdateTime(1);
        if (courseList.isEmpty()){
            log.info("no courseList");
        }
        List<CourseES> courseESList = new ArrayList<>();
        for (Course course:courseList) {
            CourseES courseES = new CourseES();
            courseES.setName(course.getCourseName());
            courseES.setDescription(course.getCourseDescription());
            courseES.setUpdateTime(new Date());
            courseESList.add(courseES);
        }
        courseESRepository.saveAll(courseESList);
    }
}
